﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HZU.ZP.Domain.IService;
using HZU.ZP.Domain.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace HZU.ZP.Controllers
{
    public class ZanController : Controller
    {
        private ICompanyService _companyService;
        private IApplicantService _applicantService;
        private IAdminService _adminService;
        private IZanService _zanService;
        public ZanController(ICompanyService companyService, IApplicantService applicantService,
            IAdminService adminService, IZanService zanService)
        {
            _companyService = companyService;
            _applicantService = applicantService;
            _adminService = adminService;
            _zanService = zanService;
        }
        // GET: /<controller>/
        public IActionResult Index()
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        public IActionResult Add(int noticeId, string email)
        {
            Zan zan = new Zan();           
            zan.NoticeId = noticeId;
            switch (User.Claims.ToList()[2].Value)
            {
                case "Admin":
                    zan.UserId = _adminService.GetAdmin(email).Id.Value;
                    zan.RoleName = "Admin";
                    break;
                case "Company":
                    zan.UserId = _companyService.GetByEmail(email).Id.Value;
                    zan.RoleName = "Company";
                    break;
                case "Applicant":
                    zan.UserId = _applicantService.GetApplicant(email).Id.Value;
                    zan.RoleName = "Applicant";
                    break;
            }            
            _zanService.Add(zan);
            return Json(AjaxResult.ReturnMessage(1, "点赞成功"));
        }

        [Authorize]
        [HttpPost]
        public IActionResult Delete(int zanId)
        {            
            _zanService.Delete(zanId);
            return Json(AjaxResult.ReturnMessage(1, "删除成功"));
        }
    }
}
